package com.jz.digital.base;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jz.digital.utils.PermissionUtil;

/**
 * @author: lyw
 * @package: com.ido.veryfit.base
 * @description: ${TODO}{ 所有Fragment的超类}
 * @date: 2018/7/16 0016
 * <p>
 * 子类如果有多个泛型，则第一个泛型必须是BasePresenter的子类
 * 在replace和add方式添加Fragemtn的时候，在调用show和hide切换Fragemtn的时候
 * Fragment可见和不可见的时候回调{@link #onVisible()#onInVisible()}
 * 如果在ViewPager里面，则此方法会失效，则需复写{@link #setUserVisibleHint(boolean)}
 * 封装了权限请求相关方法 {@link #checkSelfPermission(String...)#requestPermissions(Integer, String)#requestPermissionsSuccess(Integer)#requestPermissionsFail(Integer)}
 * <p>
 **/
public abstract class BaseCoreFragment extends Fragment{
    protected View mRootView;
    protected boolean mIsHidden = false;
    private PermissionUtil mPermissionUtil;
    //是否可以连续点击
    protected boolean mCanDoubleClick = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutResId(), container, false);
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!mIsHidden) {
            try {
                onVisible();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 初始化数据工作
     */
    protected void initData() {
    }

    /**
     * 如果不能用ButterKnife绑定的控件，可以在这里做初始化工作
     */
    protected void initView() {
    }

    /**
     *初始化监听事件
     */
    protected void initEvent(){

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mIsHidden = hidden;
        if (!hidden) {
            try{
                onVisible();
            }catch (Exception e){
                e.printStackTrace();
            }
        } else {
            onInVisible();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            try{
                onVisible();
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            onInVisible();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mIsHidden) {
            onInVisible();
        }
    }

    public boolean checkSelfPermission(String... permissions) {
        return PermissionUtil.checkSelfPermission(getContext(),permissions);
    }

    public void requestPermissions(int requestCode, PermissionUtil.RequestResult callback, String... permissions) {
        if(callback==null){
            requestPermissions(permissions,requestCode);
        }else{
            if (mPermissionUtil==null){
                mPermissionUtil=new PermissionUtil();
            }
            mPermissionUtil.setRequestResult(callback);
            PermissionUtil.requestPermissions(this, requestCode, permissions);
        }
    }

    /**
     * 不可见时回调
     */
    public void onInVisible() {
    }

    /**
     *是否可以连续点击
     * 默认禁止
     */
    protected boolean canDoubleClick(){
        return mCanDoubleClick;
    }

    /**
     * 当Fragment可见的时候回调此方法
     * 在ViewPager里面可能会失效,ViewPager嵌套时看如下方法
     * @see #setUserVisibleHint(boolean)
     */
    protected void onVisible() {
    }

    /**
     *返回布局id
     */
    protected abstract @LayoutRes int getLayoutResId();


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (mPermissionUtil!=null)mPermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
